<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use cmf\controller\BaseController;
use think\Db;
use app\admin\service\Rsa2Service;
	class WxcommonController extends BaseController
	{

		public function index(){
				/*园区列表*/	
	
		// $where['status'] = 1;
	
		// $incubator_type = $this->request->param('incubator_type');
	
		// if ($incubator_type) {
		// 	$where['incubator_type'] = $incubator_type;
		// }

		
		// if(session('incubator_id')){
		// 	$where['incubator_id']=session('incubator_id');
		// }
		// $list=Db::name('incubator')->where($where)->order("sort ASC")->select();


	


		}



		function tip(){
			$where=array();
			$arr=array();
			$str = $this->request->param('incubator_name',''); 
			if(!empty($str)){
				$where['incubator_name'] = $str;
			}
			// $incubator_id = $this->request->param('incubator_id',''); 
			// if(!empty($incubator_name)){
			// 	$where['incubator_name'] = $incubator_name;
			// }else{
			// 	$where['incubator_id'] =1;
			// }
			$where['status']=1;
			$where['online']=1;
			// $str_array=db('customer')->where('customer_name','like','%'.$str.'%')->where($where)->limit(10)->select();
			 $str_array=db('incubator')->where($where)->limit(10)->select();
			 // echo db()->getLastSql();
			foreach ($str_array as $key => $value) {
				$arr[$key]['incubator_id']=$value['id'];
				$arr[$key]['incubator_name']=$value['incubator_name'];
			}
			return json($arr);
		// $str1=implode(',', $arr);
		// echo $str1;  
		}
		function set_incubator(){
			$staff_id= $this->request->param('staff_id',''); 
			// echo $this->request->param('staff_id',1);
			// echo $staff_id;
			if(!empty($staff_id)){
				$update = db('staff_member')->where('id',$staff_id)->setField('incubator_id',$this->request->param('incubator_id',1));
				return $update;
			}else{
				return 0;
			}
			
		}



		/*支付成功调用*/
		function change_pay_status($order_sn,$state){
			//$apply_id = $this->request->param('apply_id');
			$update = db('orderlist')->where('order_sn',$order_sn)->setField('pay_status',$state);
			// if($update){
			// 	return json(array('status'=>1,'mess'=>'支付状态更新成功'));
			// }else{
			// 	return json(array('status'=>0,'mess'=>'支付状态更新失败'));
			// }
		}
 		/*获取调起支付信息*/
 		function get_pay_info(){
 			$apply_id = $this->request->param('apply_id');
 			$pay_info = db('orderlist')->where('apply_id',$apply_id)->find();
 			if($pay_info){
 				return json($pay_info);
 			}else{
 				return '暂无订单信息';
 			}
 		}

 		/*查询支付状态*/
 		function get_pay_status(){
 			$url =config('pay_status_url');
 			$data['order_sn'] = $this->request->param('order_sn');
 			$info = sqcurl($url,'post',$data,array());
 			var_dump(json_decode($info));
 			if(json_decode($info)->code ==1){
 				$this->change_pay_status($data['order_sn'],2);
 				return 1;
 			}else{
 				$this->change_pay_status($data['order_sn'],1);
 				return 0;
 			}

 		}


 		function test(){
 			//  echo date("w",1510999993);
 			// echo 8/4; echo "<br/>";
 			// echo floor(8/4);
 			// echo "<br/>";
 			// echo ceil(8/4);
 			// $a = db('orderlist')->where(array('apply_id'=>2))->value('pay_status');
 			// echo $a;
 			$time = time();
 			$condition = ' end_time <= "'.$time.'" and audit_status =1 ';
 			$infos=db('site_apply_info')->where($condition)->field('id')->select();
 			foreach ($infos as $key => $value) {
 			
 					$throw['audit_status']=3;
 					$throw['audit_remark']='审核失败';
 					$uninfo=db('site_apply_info')->alias('s')->join('site t','s.site_id=t.id')->join('staff_member m','m.id=s.staff_id')->where('s.id',$value['id'])->field('s.site_id,m.staff_name,t.site_name,s.start_time,s.end_time,m.app_userid')->find();
 					db('site_apply_info')->where('id',$value['id'])->update($throw);
 					$first='您的会议预约未通过';
 					$keyword1 = $uninfo['staff_name'];
 					$keyword2 = $uninfo['site_name'];
 					$keyword3 = date("Y-m-d H:i",$uninfo['start_time']).'--'.date("Y-m-d H:i",$uninfo['end_time']);
 					$remark = '不要灰心，下次留给您最好，欢迎再次预定';
 					$url = config('gz_send_templete');
 					$appid =config('wx_app_id');
 					$pagepath = config('wx_pagepath');
 					$return_info = push_wx_info($uninfo['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,'',$appid,$pagepath);
 				}
 			

 		}



 		/*发起支付*/
 		public function doPay(){
	//$apply_id = 1;
 			$apply_id=$this->request->param('apply_id');
 			$apply_info = db('site_apply_info')->where('id',$apply_id)->field('site_id,start_time,end_time')->find();
 			$site_id=$apply_info['site_id'];
 			$prices = db('site')->alias('s')->join('incubator i','s.incubator_id = i.id')->field('i.site_service_price,s.price,s.site_name')->where('s.id',$site_id)->find();
 			if(($prices['site_service_price']!=0 || $prices['price']!=0)  && !empty($this->request->param('token'))){
 				$start_time=$apply_info['start_time'];
 				$end_time=$apply_info['end_time'];
 				$week = date("w",$start_time);
 				//echo 	$end_time;
 				$hours = ($end_time - $start_time)/(60*60);
 				$now_hour = date("H",$end_time);
 				$now_min = date("i",$end_time);
 				$rate = $hours/4;
 				//echo $rate;
 				$whole_up = ceil($rate);
 				//echo $whole_up;
 				
 				$whole_down = floor($rate);
 				$site_rent = $whole_up*$prices['price']; 

 				$service_price = 0;
 				$start_hour = date('H',$start_time);
 				$start_min = date('i',$start_time);	
 				for($i=1;$i<= $whole_up;$i++){
			if($whole_up == 1){
				if(intval($now_min)){
				
					if($now_hour < 17 && ($week!= 6 || $week!=0)){
					
						$service_price = $prices['site_service_price'];
					}
					if($now_hour >= 17 && ($week!= 6 || $week!=0) ){
						$service_price = $prices['site_service_price']*1.5;
					}elseif($week== 6 || $week==0){
						$service_price = $prices['site_service_price']*1.5;
					}
				}else{

					if($now_hour <= 17 && ($week!= 6 || $week!=0)){
						$service_price = $prices['site_service_price'];
					}
					if($now_hour > 17 && ($week!= 6 || $week!=0) ){
						$service_price = $prices['site_service_price']*1.5;
					}elseif($week== 6 || $week==0){

						$service_price = $prices['site_service_price']*1.5;
					}
				}

			}else{
				if($week != 6 && $week!=0){
					// $start_hour +=4;
					if(intval($now_min)){
						if($now_hour >=17){

							$service_price += $prices['site_service_price']*1.5;
						}else{

							$service_price += $prices['site_service_price'];
						}
					}else{

						if($now_hour >17){

							$service_price += $prices['site_service_price']*1.5;
						}else{

							$service_price += $prices['site_service_price'];
						}
					}
					$now_hour -=4;
				}elseif($week== 6 || $week==0){
					$service_price += $prices['site_service_price']*1.5;
				}
			}

		}

 				$data['amount'] = $site_rent+$service_price;


 				$url = config('pay_url');
 				$data['body'] = $prices['site_name'];
 				$data['subject'] = $prices['site_name'];
			// $data['amount'] = $this->request->param('amount');
 				$data['token'] = $this->request->param('token');
 				$info = sqcurl($url, 'post', $data, array());
		// var_dump($data);
		        // var_dump($info);
		         if(is_object(json_decode($info))){
		         	if(json_decode($info)->code == 0){
		         		return $info;
		         	}
		         	$order_info = json_decode($info)->data->order_info;
		         	$order_arr = json_decode($info)->data->order_arr;
		         	$back['app_id'] = $order_arr->appId;
		         	$back['nonce_str'] = $order_arr->nonceStr;
		         	$back['package'] = $order_arr->package;
		         	$back['sign_type'] = $order_arr->signType;
		         	$back['time_stamp'] = (string)$order_arr->timeStamp;
		         	$back['pay_sign'] = $order_arr->paySign;
		         	$back['order_sn'] = $order_info->order_sn;
		         	$back['amount'] = $order_info->amount;
		         	$back['apply_id'] = $apply_id;
		         	db('orderlist')->where('apply_id',$apply_id)->delete();
		         	db('orderlist')->insert($back);
		         	return json($back);
		         }else{
		         	return "创建订单失败：".$info;
		         }
 				
 			}else{
 				return "缺少参数";
 			}







 		}


        function back_money(){
	        $apply_id = 390;
			$url = config('back_url');
			//$data['token'] = $this->request->param('token');
			$data['order_sn'] = $this->request->param('order_sn');
			$info = sqcurl($url,'post',$data,array());
			//var_dump($info);
			if(json_decode($info)->code == 1){
				$set = db('orderlist')->where('apply_id',$apply_id)->setField('pay_status',1);
				return $set;
			}else{
				return 0;
			}
			
		}



		// function make_rsa2(){
		// 	$rsa2 = new Rsa2Service::createSign();
		// 	$rsa2->test();
		// }


		public function get_app_user(){
			echo nonce();
			exit;
			// $data['test'] = 'tttt';
			// $data['aaaa'] = '1111';
			// $data['cccc'] = '2222';
			// $data['bbbb'] = '3333';
			$url='http://v2.crejoy.cn/user/test/info';
			$token = $this->request->param('token');
		// db()
			$data['appid']='erp';
			$data['token']=$token;
			$data['appid']='erp';
			$data['sign'] = Rsa2Service::createSign($data,config('private_key'));
			$info = sqcurl($url, 'post', $data, array());
			$info=json_decode($info,true);
			var_dump($info);
			dump($data);
			// $flag = Rsa2Service::verifySign($data,config('PUBLIC_KEY'));
			// dump($flag);
		}




		function send_mess($pay_status){

			$form_id = $this->request->param('form_id','');
			$apply_id = $this->request->param('apply_id','');	
			$apply_data = db('site_apply_info')->where('id',$apply_id)->find();	
			//var_dump($_POST);
			$remark = '';
			$eqs = '';
			$other_eqs = '';
			if(!empty($apply_data['equipments'])){
				$eq = explode(',', $apply_data['equipments']);
				foreach ($eq as $key => $value) {
					$eqs .=  get_cate_name(16,$value).' ' ;
				}
				$remark .= '   固有设备：'.$eqs.' ' ;
			}
			if(!empty($apply_data['other_equipments'])){
				$other_eq = explode(',', $apply_data['other_equipments']);
				foreach ($other_eq as $key => $value) {
					$other_eqs .=  get_cate_name(16,$value).' ' ;
				}
				$remark .= '   可选设备：'.$other_eqs.' ' ;
			}
			if($pay_status){
				$remark .='留言：'.$apply_data['remark'].'。'. '请尽快处理(该预约已支付)';
			}else{
				$remark .= '留言：'.$apply_data['remark'].'。'.'请尽快处理(该预约未支付)';
			}									
			// if($apply && $pay_info==0){
			 // file_put_contents('/hl/log.txt',print_r('11111',true),FILE_APPEND);
				if($apply_data['is_audit'] == 2){
					$staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name')->find();
					$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
					$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
				// file_put_contents('/hl/log.txt',print_r('2222221',true),FILE_APPEND);

				 //var_dump($receive_info);
			//	echo $site_type_id .'###';
				// file_put_contents('/hl/log.txt',print_r('bbbbbbb',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($site_type_id['site_type_id'],true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r('ccccccc',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
					foreach ($receive_infos as $key => $receive_info) {
						if(in_array($site_type_id['site_type_id'], explode(',', $receive_info['receive_site']))){
							$first='有会议室预约请求待审核';
							$keyword1 = $staff_info['staff_name'];
							$keyword2 = $site_type_id['site_name'];
							$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
							$remark = $remark;
							$url = config('gz_send_templete');
							$url1 = config('meeting_audit_list').'?wx_secrect='.$receive_info['id'];
							$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
						}
					}
				}else{
				
					$staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name,s.apply_member')->find();
				//	echo db()->getLastsql();
					//echo 3332;
					//var_dump($staff_info);
					//exit;
					if($pay_status){


						if($staff_info['apply_member'] ==3){
							
							$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
							$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
							// echo db()->getLastsql();
							// var_dump($receive_infos);
							foreach ($receive_infos as $key => $receive_info) {
							
								if($receive_info['receive_user'] ==2 ){

									$first='有企业员工待审核';
									$keyword1 = $staff_info['staff_name'];
									$keyword2 = $site_type_id['site_name'];
									$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
									$remark = $remark;
									$url = config('gz_send_templete');
									$url1 =  config('staff_audit_list').'?wx_secrect='.$receive_info['id'];
									$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
								
								}
							}
							
						}else{
							if($eqs || $other_eqs || !empty($apply_data['remark'])){





							// $staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name')->find();
								$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
								$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
								foreach ($receive_infos as $key => $receive_info) {
									if(in_array($site_type_id['site_type_id'], explode(',', $receive_info['receive_site']))){
										$first='有会议室预约请求待审阅';
										$keyword1 = $staff_info['staff_name'];
										$keyword2 = $site_type_id['site_name'];
										$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
										$remark = $remark;
										$url = config('gz_send_templete');
										$url1 = config('meeting_audit_list').'?wx_secrect='.$receive_info['id'];
										$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
									
									}
								}

							}
						}
					}else{
						$staff_info = db('staff_member')->alias('s')->join('customer c','s.customer_id = c.id')->where('s.id',$apply_data['staff_id'])->field('c.incubator_id,s.staff_name')->find();
						$site_type_id = db('site')->where('id',$apply_data['site_id'])->field('site_type_id,site_name')->find();
						$receive_infos = db('user')->alias('u')->join('role_user r' ,'u.id = r.user_id')->where(array('u.incubator_id'=> $staff_info['incubator_id'],'r.role_id'=>4))->field('u.receive_site,u.receive_user,u.id,u.app_userid')->select();
				// file_put_contents('/hl/log.txt',print_r('2222221',true),FILE_APPEND);

				 //var_dump($receive_info);
			//	echo $site_type_id .'###';
				// file_put_contents('/hl/log.txt',print_r('bbbbbbb',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($site_type_id['site_type_id'],true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r('ccccccc',true),FILE_APPEND);
				// file_put_contents('/hl/log.txt',print_r($receive_infos,true),FILE_APPEND);
						db('site_apply_info')->where('id',$apply_id)->setField('audit_status',1);
						foreach ($receive_infos as $key => $receive_info) {
							if(in_array($site_type_id['site_type_id'], explode(',', $receive_info['receive_site']))){
								$first='有会议室预约请求待审核';
								$keyword1 = $staff_info['staff_name'];
								$keyword2 = $site_type_id['site_name'];
								$keyword3 = date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']);
								$remark = $remark;
								$url = config('gz_send_templete');
								$url1 = config('meeting_audit_list').'?wx_secrect='.$receive_info['id'];
								$return_info = push_wx_info($receive_info['app_userid'],$first,$keyword1,$keyword2,$keyword3,$remark,$url,$url1);
							}
						}
					}
				}
				if($form_id){
					$site_name = db('site')->where('id',$apply_data['site_id'])->value('site_name');
					$app_userid = db('staff_member')->where('id',$apply_data['staff_id'])->value('app_userid');
					$is_focus = db('focus')->where('app_userid',$app_userid)->value('is_focus');
					$remark = '未关注公众账号，点击进入小程序进入个人页面进行关注';
					if($is_focus != 1){
						$return_info =send_info($site_name,$remark,date("Y-m-d H:i",$apply_data['start_time']).'--'.date("Y-m-d H:i",$apply_data['end_time']),'pages/mine/mine',$app_userid,$form_id);

					}
				}
				echo $return_info;
				exit;
				// if($pay_info){
				// 	return $pay_info;
				// }else{
				// 	return 1;
				// }

			// }else{
			// 	return 0;
			// }





























		}



		function is_pay(){

			$pay_status = $this->request->param('pay_status');
			$this->send_mess($pay_status);






		}






}